define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    // require('commonjs');
    // require('globaljs');
    
    require('../su/accordion/oaTabCtrl');
    

    require('oa/js/task/taskContentCtrl');

    $.widget('rk.taskPanelCtrl', $.rk.oaPanelCtrl, {
        _create: function () {
            var me = this;
            var elem = me.element;
            me._initTaskPanel();
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _initTaskPanel: function () {
            var me = this;
            var elem = me.element;
            var url = '/json/oa_task/switch-view.action';
            $('#taskPanelContentDiv .oa-task-content-list').unScroll();
            rk.post(url, {viewType: 1}).done(function (json) {
                if (json.status == 0) {
                    json.data.currentUserId = SESSION.user.id;
                    var html = rk.templateText(require('oa/tmpl/task/tpl_task_panel.tpl'), json.data);
                    elem.html(html);
                    me._initCreateMenu();
                    me._initAccordion();
                    me._bindHandler();
                    if (json.data.allCount < 1) {
                        html = rk.templateText(require('oa/tmpl/task/tpl_task_panel_default_list.tpl'), {});
                        $('#taskPanelContentDiv').find('.oa-task-content-list').html(html);
                    }
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _loadDataByViewType: function (viewType) {
            var me = this;
            var elem = me.element;
            var url = '/json/oa_task/switch-view.action';
            $('#taskPanelContentDiv .oa-task-content-list').unScroll();
            $('#taskPanelContentDiv').beLoading();
            rk.post(url, {
                viewType: viewType
            }).done(function (json) {
                if (json.status == 0) {
                    $('#showFinishedTasks').show();
                    var taskHtml;
                    if (viewType == 1) {
                        json.data.currentUserId = SESSION.user.id;
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_my_task.tpl'), json.data);
                    } else if (viewType == 3) {
                        if (json.data.assignedUsers) {
                            $.each(json.data.assignedUsers, function (i, o) {
                                if (o.icon == '') {
                                    o.icon = DEFAULTS.USER_DEFAULT_ICON;
                                }
                            });
                        }
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_assign_task.tpl'), json.data);
                    }
                    $('#taskPanelContentDiv').html(taskHtml);
                    if (json.data.allCount < 1) {
                        $('#taskPanelContentDiv').unLoading();
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_default_list.tpl'), {});
                        $('#taskPanelContentDiv').find('.oa-task-content-list').html(taskHtml);
                        return;
                    }
                    me._initAccordion();
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _loadDataByShowType: function (showType) {
            var me = this;
            var elem = me.element;
            var url = '/json/oa_task/switch-group-by.action';
            $('#taskPanelContentDiv .oa-task-content-list').unScroll();
            $('#myAssignedTasksAccordionDiv').beLoading();
            rk.post(url, {
                showType: showType
            }).done(function (json) {
                if (json.status == 0) {
                    var taskHtml;
                    if (showType == 1) {
                        if (json.data.assignedUsers) {
                            $.each(json.data.assignedUsers, function (i, o) {
                                if (o.icon == '') {
                                    o.icon = DEFAULTS.USER_DEFAULT_ICON;
                                }
                            });
                        }
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_assign_by_user.tpl'), json.data);
                    } else if (showType == 2) {
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_assign_by_time.tpl'), json.data);
                    }
                    $('#myAssignedTasksAccordionDiv').html(taskHtml);
                    if (json.data.allCount < 1) {
                        $('#myAssignedTasksAccordionDiv').unLoading();
                        taskHtml = rk.templateText(require('oa/tmpl/task/tpl_task_panel_default_list.tpl'), {});
                        $('#myAssignedTasksAccordionDiv').html(taskHtml);
                        return;
                    }
                    me._initAccordion();
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
            });
        },
        _initCreateMenu: function () {
            var me = this;
            var elem = me.element;
            var header = me.getPanelHeader();
            header.find('#createTaskBtn').rkEasyMenu({
                menuId: "createTaskMenu",
                onSelect: function (li) {
                    var act = li.attr('act');
                    me._handleCreateClick(act);
                }
            });
        },
        _initAccordion: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var content = me.getPanelContent();
            content.find('h3[taskType]').off("click").on("click", function () {
                var $el = $(this);
                $el.toggleClass("expand");
                if ($el.hasClass("expand")) {
                    $el.attr("title", rk.i18n('TASK_CLICK_COLLECT'));
                    elem.find("div[taskType=" + $el.attr("taskType") + "TasksDiv]").slideDown(350, function () {
                        me._showFooterShadow();
                    });
                } else {
                    $el.attr("title", rk.i18n('TASK_CLICK_OPEN'));
                    elem.find("div[taskType=" + $el.attr("taskType") + "TasksDiv]").slideUp(350, function () {
                        me._showFooterShadow();
                    });
                }
            });
            if (opt.taskType) {
                content.find('h3[taskType=' + opt.taskType + ']').trigger('click');
            } else {
                content.find('h3[taskType=today]').trigger('click');
            }
            rk.onResize(elem).bind('handleGlobalResize', function(){
                me._showFooterShadow();
            });

            //滚动条
            $('#taskPanelContentDiv .oa-task-content-list').beScroll();
            //计算列表的偏移高度
            var offset = $('#taskPanelContentDiv .oa-task-content-toolbar').outerHeight();
            offset = offset ? offset : 0;
            $('#taskPanelContentDiv .oa-task-content-list').css('height', 'calc(100% - '+offset+'px)')
        },
        _bindHandler: function () {
            var me = this;
            var elem = me.element;
            elem.on('click', 'div[_taskId]', function () {
                me._handleRowClick($(this));
            });
            elem.on('click', 'a[_taskId]', function (event) {
                event = event || window.event;
                if(event.stopPropagation) { //W3C阻止冒泡方法
                    event.stopPropagation();
                } else {
                    event.cancelBubble = true; //IE阻止冒泡方法
                }
                var $el = $(this);
                if ($el.hasClass('done')) {
                    rk.confirm(rk.i18n('TASK_IS_RESTART'), function () {
                        me._changeStatus($el);
                    });
                } else {
                    me._changeStatus($el);
                }
            });
            elem.on('click', 'span[act=taskTab]', function () {
                var viewType = $(this).attr('viewType');
                rk.oa.closeCurtain();
                me._loadDataByViewType(viewType);
                elem.find('span[act=taskTab]').removeClass('active');
                $(this).addClass('active');
                $('#showFinishedTasks').attr('viewType', viewType);
                $('#showFinishedTasks').hide();
            });
            elem.on('click', 'a[act=taskGroupBy]', function () {
                var showType = $(this).attr('showType');
                me._loadDataByShowType(showType);
                elem.find('a[act=taskGroupBy]').removeClass('active');
                $(this).addClass('active');
            });
            elem.on('click', 'a[act=taskCurtain]', function () {
                me._handleTaskCurtainClick($(this));
            });
            elem.on('click','.oa-close-panel',function(){
                rk.oa.closePanel();
            });
        },
        _handleTaskCurtainClick: function ($taskCurtainA) {
            //查看已完成的任务传viewType,未接受和已拒绝的任务传aType
            rk.oa.popupCurtain({
                top: 70,//不遮盖按钮
                onSuccess: function (curtainElem) {
                    curtainElem.taskCurtainCtrl({
                        aType: $taskCurtainA.attr('aType'),
                        viewType: $taskCurtainA.attr('viewType')
                    });
                }
            });
        },
        _handleRowClick: function (row) {
            var me = this;
            var elem = me.element;
            var taskId = row.attr('_taskId');
            $('div[_taskId]').removeClass('show-content-hover');
            row.addClass('show-content-hover');
            me.popupContent({
                onSuccess: function (contentElem) {
                    contentElem.taskContentCtrl({
                        taskId: taskId
                    });
                }
            });
        },
        _handleCreateClick: function (act) {
            var me = this;
            var elem = me.element;
            var title = '';
            var forSelf = (act == 'selfCreateTask');
            if (forSelf) {
                title = rk.i18n('TASK_TO_ME');
            } else {
                title = rk.i18n('TASK_TO_OTHER');
            }
            rk.dialog({
                title: title,
                width: 570,
                success: function (dialogElem) {
                    dialogElem.createTaskDialogCtrl({
                        forSelf: forSelf,
                        onSuccess: function () {
                            var $activieTab = elem.find('span[act=taskTab].active');
                            var vType = $activieTab.attr('viewtype');
                            if (vType == 1 && forSelf) {
                                $activieTab.trigger('click');
                            } else if (vType == 3 && !forSelf) {
                                $activieTab.trigger('click');
                            }
                        }
                    });
                },
                footer: [{
                    act: 'nextstep', visible: true, text: rk.i18n('TASK_NEXT_STEP')
                }, {
                    act: 'prevstep', color: 'white', visible: true, text: rk.i18n('TASK_PRE_STEP')
                }, {
                    act: 'save', visible: true, text: rk.i18n('TASK_CONFIRM')
                }]
            });
        },
        _changeStatus: function ($el) {
            var checked = $el.hasClass('done');
            $.post('/json/crm_task/change-status.action', {
                taskId: $el.attr('_taskId'),
                taskStatus: checked ? 1 : 2
            }, null, 'json').done(function(json) {
                if (json.status == 0) {
                    if (checked) {
                        $el.removeClass('done').addClass('normal').attr('title', rk.i18n('TASK_CLICK_FINISHED'));
                        $('a[taskId=' + $el.attr('_taskId') + ']').removeClass('done').addClass('normal').attr('title', rk.i18n('TASK_CLICK_FINISHED'));
                        $.msg(rk.i18n('TASK_RESTART_SUCCESS'), 1);
                    } else {
                        $el.removeClass('normal').addClass('done').attr('title', rk.i18n('TASK_CLICK_CANCEL_FINISHED'));
                        $('a[taskId=' + $el.attr('_taskId') + ']').removeClass('normal').addClass('done').attr('title', rk.i18n('TASK_CLICK_CANCEL_FINISHED'));
                        $.msg(rk.i18n('TASK_HAVE_FINISHED'), 1);
                    }
                } else {
                    $.msg(rk.i18n('TASK_UPDATE_STATUS_ERROR'));
                }
            }).fail(function(def, code, message) {
                def.status != 0 && $.msg(rk.i18n('LOAD_ERROR'));
            });
        },
        _showFooterShadow: function(){
            var me = this;
            var elem = me.element;
            var accordion = $('#taskPanelContentDiv .oa-panel-accordion');
            var accHeight = accordion.outerHeight();
            var parentHeight = elem.find('.oa-task-content-list').innerHeight();
            var diff = parentHeight - accHeight;

            var footer = elem.find('.oa-task-footer');
            var footerHeight = footer.outerHeight();
            if(footerHeight > diff){
                footer.addClass('has-shadow');
            }else{
                footer.removeClass('has-shadow');
            }
        }
    });

    return $;
});